/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validador;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import connezione.PooledConnection;
import hirondelle.date4j.DateTime;
import java.sql.SQLException;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import login.BeanUser;

/**
 *
 * @author sestari
 */
public class BeanValidador {

    /** Creates a new instance of BeanValidador */
    public BeanValidador() {
    }

    

    private boolean mailSyntaxCheck(String email) {
        // Create the Pattern using the regex
        Pattern p = Pattern.compile(".+@.+\\.[a-z]+");

        // Match the given string with the pattern
        Matcher m = p.matcher(email);

        // check whether match is found
        boolean matchFound = m.matches();

        StringTokenizer st = new StringTokenizer(email, ".");
        String lastToken = null;
        while (st.hasMoreTokens()) {
            lastToken = st.nextToken();
        }

        // validate the country code
        if (matchFound && lastToken.length() >= 2
                && email.length() - 1 != lastToken.length()) {

            return true;
        } else {
            return false;
        }

    }

    public void verificaMail(FacesContext context, UIComponent component, Object value) {
        String campo = null;
        campo = value.toString();


        if (!mailSyntaxCheck(campo)) {
            FacesMessage msg = lingua.BeanLingua.getMessage("message.message", "errorMail", null);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }

    }

    public void validateDate(FacesContext context, UIComponent component, Object value) {
        DateTime date = new DateTime((String) value);
        try {
            int d = date.getDay();
            int m = date.getMonth();
            int y = date.getYear();
        } catch (Exception e) {
            FacesMessage msg = lingua.BeanLingua.getMessage("message.message", "errorDate", null);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }


    }
    
    
        public void verificaUserName(FacesContext context, UIComponent component, Object value) throws SQLException {
        String campo = null;
        campo = value.toString();
        
        PooledConnection conn = (PooledConnection) ((BeanUser) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user")).getCon();
        Statement stmt = (Statement) conn.createStatement();
        ResultSet result = (ResultSet) stmt.executeQuery("SELECT * FROM `db_user15`.`Utenti` where username= \"" + campo+"\"");
        if (result.next()){
            FacesMessage msg = lingua.BeanLingua.getMessage("message.message", "erroUsername", null);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }       
    }
       
}
